import {IotEventExt} from './ioteventext.js'
import store from '../../store'
import {listdatag} from '../public/regional';
import axios from 'axios'
console.log(store.state.token,store.state.root)
let ExtSupport= false;
let gEvent=[];
var e1=undefined;
function EventCB(event, userdata)
{   
   
    var msgevent = JSON.parse(event);
    let timeitem={
            Token: msgevent.strDevToken,
            Type: msgevent.type,
            UUID:msgevent.strUUID,
            Time:msgevent.strTime,                      
            Detail:event
        };
    gEvent.unshift(timeitem);
    //console.log(gEvent.length)
}
let url = store.state.IPPORT + "/api/v1/GetAdvSystemInfo?session="+ store.state.token;
console.log(url);
axios.get(url).then(result=>{
    if (result.status == 200){
        console.log(result.data.bExtSupport);
        if (result.data.bExtSupport) {
            ExtSupport=true
            setTimeout(function(){
                clearInterval(acc)
            },2000)
        }else{
            clearInterval(acc)
        }
    } 
}).catch((err) =>{
    console.log('GetSrc failed', err);
    clearInterval(acc);
});
let obj = {types:["H5S_EVENT_ONLINE","H5S_EVENT_OFFLINE","H5S_EVENT_MOTION"]};
let acc =setInterval(() =>{
    if (ExtSupport) {
        let eventhost=window.location.host.split(':');
        var conf1 = {
            protocol: window.location.protocol, //http: or https:
            host:store.state.WSROOT, //localhost:8080
            rootpath:'/', // '/'
            callback: EventCB, 
            userdata: null, // user data
            session: store.state.token, //session got from login
            Subscription: obj,
        };
        e1 = new IotEventExt(conf1);
        e1.connect();
        console.log(e1.wsSocket);
    }
},1000)
// var url =store.state.IPPORT + "/api/v1/GetSrc?session=" +  store.state.token;

// fetch(url).then((result) => {
//     console.log("a", result);
//     if (result.status == 200) {
//       var itme = result.data.src;
//       for (var i = 0; i < itme.length; i++) {
//           obj.token=itme[i].strToken;
//           obj.types = item[i].type;
//       }
//     }
// })

// function events(){
    // if(store.state.token==null){
    //     return false;
    // }
    // if (e1.wsSocket.readyState===1) {
    // e1.wsSocket.send(JSON.stringify(store.state.subscribeEvent));
    // }
// }
// events();
export default{gEvent,e1}